package com.taiyi.renaissancemall.ui.mine;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;

import com.taiyi.renaissancemall.Utils.ToastUtils;
import com.taiyi.renaissancemall.Utils.Utils;
import com.taiyi.renaissancemall.base.BasePresent;
import com.taiyi.renaissancemall.base.MyApp;
import com.taiyi.renaissancemall.bean.BaseResultBean;
import com.taiyi.renaissancemall.bean.PersonInfoBean;
import com.taiyi.renaissancemall.bean.VerifiedBean;
import com.taiyi.renaissancemall.bean.VersionUpdateBean;
import com.taiyi.renaissancemall.net.BaseUrl;
import com.taiyi.renaissancemall.net.HttpUtils;
import com.taiyi.renaissancemall.net.callbck.JsonCallback;
import com.taiyi.renaissancemall.ui.login.LoginActivity;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Response;

public class MineFragmentPresent extends BasePresent<MineFragmentView> {

    public void getPersonInfo() {
        Map<String, String> map = new HashMap<>();
        HttpUtils.postRequest(BaseUrl.GET_PERSON_INFO, this, map, new JsonCallback<BaseResultBean<PersonInfoBean>>() {
            @Override
            public BaseResultBean<PersonInfoBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<PersonInfoBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    view.getPersonInfoSuccess(response.body().data);
                } else if (response.body().code == -1) {
                    ToastUtils.showShortToast(response.body().msg_cn);
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.getPersonInfoFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<PersonInfoBean>> response) {
                super.onError(response);
            }
        });
    }

    public void toSign(String buqian) {
        Map<String, String> map = new HashMap<>();
        map.put("buqian", buqian);
        HttpUtils.postRequest(BaseUrl.USER_SIGN, this, map, new JsonCallback<BaseResultBean<VerifiedBean>>() {
            @Override
            public BaseResultBean<VerifiedBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<VerifiedBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    view.toSignSuccess(response.body().data);
                } else if (response.body().code == -1) {
                    ToastUtils.showShortToast(response.body().msg_cn);
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.toSignFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<VerifiedBean>> response) {
                super.onError(response);
            }
        });
    }

    public void checkVersionUpdate() {
        Map<String, String> map = new HashMap<>();
        map.put("phoneVersion", "android");
        HttpUtils.postRequest(BaseUrl.CHECK_VERSION_UPDATE, this, map, new JsonCallback<BaseResultBean<VersionUpdateBean>>() {
            @Override
            public BaseResultBean<VersionUpdateBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<VersionUpdateBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    view.getVersionSuccess(response.body().data);
                } else {
                    view.getVersionFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<VersionUpdateBean>> response) {
                super.onError(response);
            }
        });
    }
    long appVersionCode = 0;
    public long getAppVersionCode() {
        PackageInfo packageInfo;
        try {
            packageInfo = MyApp.getInstance().getApplicationContext().getPackageManager().getPackageInfo(MyApp.getInstance().getPackageName(), 0);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                appVersionCode = packageInfo.getLongVersionCode();
            } else {
                appVersionCode = packageInfo.versionCode;
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return appVersionCode;
    }
}
